package controller

import (
	"github.com/savsgio/atreugo/v11"
	jsoniter "github.com/json-iterator/go"
	"{{.projectName}}/model"
	"{{.projectName}}/service"
)

type {{.structName}}Controller struct{
    {{.structName}}Service *service.{{.structName}}Service
}

func New{{.structName}}Controller() *{{.structName}}Controller {
    return &{{.structName}}Controller{
        {{.structName}}Service:service.New{{.structName}}Service(),
    }
}

{{- if eq .isUser true}}
func ({{.objectName}}Ctl *{{.structName}}Controller) Login(ctx *atreugo.RequestCtx) error {
    var ufi model.UserLoginInfo

    err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(ctx.PostBody(), &ufi)
    if err != nil{
        return ctx.JSONResponse(map[string]interface{}{"code":500, "message":err.Error(), "data":nil})
    }

    return ctx.JSONResponse(map[string]interface{}{"code":"200", "data":{{.objectName}}Ctl.{{.structName}}Service.Login(&ufi), "message":"成功"})
}
{{- end}}

func ({{.objectName}}Ctl *{{.structName}}Controller) Created{{.structName}}(ctx *atreugo.RequestCtx) error {
    var o model.{{.structName}}

    err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(ctx.PostBody(), &o)
    if err != nil{
        return ctx.JSONResponse(map[string]interface{}{"code":500, "message":err.Error(), "data":nil})
    }

    return ctx.JSONResponse({{.objectName}}Ctl.{{.structName}}Service.Created{{.structName}}(&o))
}

func ({{.objectName}}Ctl *{{.structName}}Controller) Update{{.structName}}(ctx *atreugo.RequestCtx) error {
    var o model.{{.structName}}

    err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(ctx.PostBody(), &o)
    if err != nil{
        return ctx.JSONResponse(map[string]interface{}{"code":500, "message":err.Error(), "data":nil})
    }

    return ctx.JSONResponse({{.objectName}}Ctl.{{.structName}}Service.Update{{.structName}}(&o))
}

func ({{.objectName}}Ctl *{{.structName}}Controller) Delete{{.structName}}(ctx *atreugo.RequestCtx) error {
    id, _ := ctx.UserValue("pageNo").({{.pkType}})

    return ctx.JSONResponse({{.objectName}}Ctl.{{.structName}}Service.Delete{{.structName}}(id))
}

func ({{.objectName}}Ctl *{{.structName}}Controller) Find{{.structName}}All(ctx *atreugo.RequestCtx) error {
    condition := make(map[string]interface{})

    err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(ctx.PostBody(), condition)
    if err != nil{
        return ctx.JSONResponse(map[string]interface{}{"code":500, "message":err.Error(), "data":nil})
    }

    return ctx.JSONResponse(map[string]interface{}{"code":"200", "data":{{.objectName}}Ctl.{{.structName}}Service.Find{{.structName}}All(condition), "message":"成功"})
}

func ({{.objectName}}Ctl *{{.structName}}Controller) Find{{.structName}}One(ctx *atreugo.RequestCtx) error {
    id, _ := ctx.UserValue("pageNo").({{.pkType}})

    return ctx.JSONResponse(map[string]interface{}{"code":"200", "data":{{.objectName}}Ctl.{{.structName}}Service.Find{{.structName}}One(id), "message":"成功"})
}
